package cn.swing.ushare.passport.config.security;

import cn.swing.ushare.passport.initException.SimpleAccessDeniedHandler;
import cn.swing.ushare.passport.initException.SimpleAuthenticationEntryPoint;
import cn.swing.ushare.passport.security.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class UshareSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        // TODO 关闭spring security
//        web.ignoring().antMatchers("/**");
//    }
//


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //禁用防跨域攻击
        http.csrf().disable();
        http.exceptionHandling()
                .authenticationEntryPoint(new SimpleAuthenticationEntryPoint()).accessDeniedHandler(new SimpleAccessDeniedHandler());


        System.out.println("白名单设置");
        String[] urls = {
//                "/**",
                "/admins/loginBefore",
                "/articlePictures/**",
                "/articles/uploadArticlePicture",
                "/users/selectUsernameByNickname",
                "/elements/listMenuBar",
                "/articles/categoryMenu",
//                "/comments/getUnreadMessagesCounts",
                "/articles/articlesDetailsHome",
                "/articles/addArticleViews",
                "/elements/listCarousel",
                "/articles/articleChannel",
                "/users/userRegister",
//                "/login",
                "/articles/hotArticlesDetails",
                "/hot",
                "/category",
                "/guestbook/list",
                "/users/login",
                "/admins/login",
                "/doc.html",
                "/**/*.js",
                "/**/*.css",
                "/swagger-resources",
                "/v2/api-docs",
                "/favicon.ico",
                "/users/currentLogin",
                "/articles/addUserLoveCount",
                "/articles/subUserLoveCount",
                "/users/addUserFollowers",
                "/users/subUserFollowers",
                "/users/addUserFans",
                "/users/subUserFans"
        };

        //配置个请求路径的认证与授权
        http.authorizeRequests()//请求需要授权才可以授权
                .antMatchers(urls) //匹配一些路径
                .permitAll()
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll()//允许直接访问(不需要认证和授权)
                .anyRequest() //匹配除了以上配置的其他请求
                .authenticated(); //都需要认证

//        http.authorizeRequests()  //请求需要授权才可以访问
//                .antMatchers(urls).permitAll()  //匹配一些路径
//                .antMatchers(HttpMethod.OPTIONS)
//                .permitAll()
//                .anyRequest() //除了以上配置其他请求
//                .authenticated(); //都要验证

        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }


}
